﻿/*
Project Name : Menfram
Module Name  : DAO
Date  : 07/06/2013
responsible : Mateus Neto
Obs  : The license of this software is Mozilla Public License 1.1 
contact : mentrixmax@gmail.com
*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Common;
using Npgsql;
using MySql.Data.MySqlClient;

namespace Menfram.Implementation.DAL
{
    //Class responsible for the ability of the framework to connect to a mysql database.
    public class MysqlDAO : IAbstractSQL
    {
        #region IAbstractSQL Members

        /// <summary>
        /// Method that creates a command to execute SQL queries
        /// </summary>
        public System.Data.Common.DbCommand Command
        {
            get;
            set;
        }

        /// <summary>
        /// Method that creates a connection with database
        /// </summary>
        public System.Data.Common.DbConnection Connection
        {
            get { return getConection(); }
        }

        /// <summary>
        /// Method that creates a connection with database
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns>

        public System.Data.Common.DbCommand getCommand(string sql)
        {
            Command = new MySqlCommand(sql, (MySqlConnection)Connection);
            return Command;
        }

        /// <summary>
        /// method used to mount the queries in the database
        /// </summary>
        public System.Collections.Hashtable listOfTheParam
        {
            get;
            set;

        }

        /// <summary>
        /// the method that set the parameteres 
        /// </summary>
        public void setParameteres()
        {
            Command.Parameters.Clear();
            MySqlCommand com = (MySqlCommand)Command;
            if (listOfTheParam != null)
            {
                foreach (string key in listOfTheParam.Keys)
                {
                    if (listOfTheParam[key] == null)
                    {

                        com.Parameters.AddWithValue(key, DBNull.Value);

                    }
                    else
                    {
                        com.Parameters.AddWithValue(key, listOfTheParam[key]);
                    }
                }
            }
        }

        /// <summary>
        /// method with get the transaction with the database
        /// </summary>
        public System.Data.Common.DbTransaction Transaction
        {
            get;
            set;
        }

        private static PostgreDAO dao = null;


        /// <summary>
        /// implementation of the pattner Singleton
        /// </summary>
        public IAbstractSQL getInstance()
        {
            if (dao == null)
            {
                dao = new PostgreDAO();
            }
            return dao;
        }

        /// <summary>
        /// Atribute Connection;
        /// </summary>
        private static MySqlConnection conn = null;


        #endregion

     
        /// <summary>
        /// Propertie url of the Connetion
        /// </summary>
        private String PUrlConnection;

        public String UrlConnection
        {
            get { return PUrlConnection; }
            set { PUrlConnection = value; }
        }



        /// <summary>
        /// method that get the MysqlConnection
        /// </summary>
        /// <returns></returns>
        public MySqlConnection getConection()
        {
            if (conn == null)
            {
                conn = new MySqlConnection(UrlConnection);
            }
            this.openConnection();
            return conn;
        }

        /// <summary>
        /// the method with open the connection
        /// </summary>
        /// <returns></returns>
        private bool openConnection()
        {
            try
            {
                if (conn.State == System.Data.ConnectionState.Closed)
                {
                    conn.Open();
                    return true;
                }

            }
            catch (DbException e)
            {
                conn = null;
                throw e;
            }
            return false;
        }

        /// <summary>
        /// the method with close the connection 
        /// </summary>
        /// <returns></returns>
        public bool closeConnection()
        {
            try
            {
                if (conn != null && conn.State == System.Data.ConnectionState.Open)
                {
                    conn.Close();
                    return true;
                }

            }
            catch (DbException e)
            {

                throw e;
            }
            return false;
        }
    }
}
